﻿
namespace Apache.Cassandra.Client {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Apache.Cassandra;

    [CLSCompliant(false)]
    public static class SliceExtensions {

        public static object GetItem(this IEnumerable<ColumnOrSuperColumn> slice, int i) {
            if (slice == null) {
                return null;
            }

            return slice.ElementAt(i).GetItem();
        }

        public static object GetValue(this IEnumerable<ColumnOrSuperColumn> slice, int i) {
            if (slice == null) {
                return null;
            }

            return slice.ElementAt(i).GetValue();
        }

        public static object GetItem(this IEnumerable<ColumnOrSuperColumn> slice, byte[] name) {
            if (name == null) {
                throw new ArgumentNullException("name");
            }

            if (slice == null) {
                return null;
            }

            foreach (ColumnOrSuperColumn i in slice) {
                if (name.SequenceEqual(i.GetName())) {
                    return i.GetItem();
                }
            }
            return null;
        }

        public static object GetValue(this IEnumerable<ColumnOrSuperColumn> slice, byte[] name) {
            if (name == null) {
                throw new ArgumentNullException("name");
            }

            if (slice == null) {
                return null;
            }

            foreach (ColumnOrSuperColumn i in slice) {
                if (name.SequenceEqual(i.GetName())) {
                    return i.GetValue();
                }
            }
            return null;
        }

        public static List<ColumnOrSuperColumn> GetSlice(this IEnumerable<KeyValuePair<byte[], List<ColumnOrSuperColumn>>> slices, byte[] key) {
            if (null == key) {
                throw new ArgumentNullException("key");
            }
            if (null == slices) {
                return null;
            }

            foreach (KeyValuePair<byte[], List<ColumnOrSuperColumn>> i in slices) {
                if (key.SequenceEqual(i.Key)) {
                    return i.Value;
                }
            }
            return null;
        }

        public static object GetItem(this IEnumerable<KeyValuePair<byte[], List<ColumnOrSuperColumn>>> slices, byte[] key, byte[] name) {
            return slices.GetSlice(key).GetItem(name);
        }

        public static object GetValue(this IEnumerable<KeyValuePair<byte[], List<ColumnOrSuperColumn>>> slices, byte[] key, byte[] name) {
            return slices.GetSlice(key).GetValue(name);
        }
    }
}
